<template>
  <div class="main-content">
    <h1 style="margin: 5px 0">机构组别业绩排名表</h1>
    <div class="screen-frame clearfix">
      <div class="row">
        <div class="col-lg-6">
          <div class="form-horizontal">
            <div class="form-group">
              <label class="col-md-4 col-sm-5 control-label">楼盘名称：</label>
              <div class="col-md-6 col-sm-7">
                <el-select class="" v-model="params.premisesId" filterable placeholder="请选择" clearable="true" style="width:100%">
                  <el-option v-for="item in premisesList" :key="item.premisesId" :label="item.premisesName" :value="item.premisesId">
                  </el-option>
                </el-select>
              </div>
            </div>
          </div>
        </div>
        <div class="col-lg-5">
          <div class="form-horizontal">
            <div class="form-group">
              <label class="col-md-3 col-sm-2 control-label">时间区间：</label>
              <div class="col-md-4 col-sm-5">
                <DatePicker type="date" placeholder="开始日期" v-model="params.beginDate" style="width:100%;"></DatePicker>
              </div>
              <div class="col-md-4 col-sm-5">
                <DatePicker type="date" placeholder="结束日期" v-model="params.endDate" style="width:100%;"></DatePicker>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-lg-6">
          <div class="form-horizontal">
            <div class="form-group">
              <label class="col-md-4 col-sm-5 control-label">门店名：</label>
              <div class="col-md-4 col-sm-5">
                <Select v-model="storeId" filterable @on-change="findReportList">
                  <Option v-for="item in storeList" v-model="item.id" :key="item.id">{{ item.departmentName }}</Option>
                </Select>
              </div>
            </div>
          </div>
        </div>
        <div class="col-lg-5">
          <div class="form-horizontal">
            <div class="form-group">
              <label class="col-md-3 col-sm-2 control-label">组别：</label>
              <div class="col-md-4 col-sm-5">
                <Input v-model="params.groupName" placeholder="请输入组别" />
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-lg-5">
          <div class="form-horizontal">
            <div class="form-group">
              <label class="col-md-4 col-sm-2 control-label" style="padding-top: 0;">有效数据：</label>
              <div class="col-md-8 col-sm-5">
                <Checkbox v-model="showValidDataFlag">过滤无业绩统计</Checkbox>
              </div>
            </div>
          </div>
        </div>
        <div class="col-lg-8">
          <div class="form-group">
            <label class="col-md-3 col-sm-2 control-label"></label>
            <div class="col-md-9 col-sm-10">
              <button type="button" class="btn btn-theme" v-on:click="list(1)">查询</button>
              <button type="button" class="btn btn-theme" v-on:click="exportInfo" v-res="{code:'menu_0007_0006_0001'}">导出</button>
              <a :href="exportUrl" v-show="exportBtnShow" download="组别业绩排名.xlsx" style="color: yellowgreen; padding-left: 20px;">
                点击下载
                <Icon type="md-cloud-download" size="16" />
              </a>
            </div>
          </div>
        </div>
      </div>

    </div>
    <div class="row" style="text-align: center;color: #f87242;" v-show="!show">
      没有查询到相应的数据
    </div>
    <div class="table table-striped table-bordered" v-show="show">

      <div class="table-responsive">
        <table class="table table-striped table-bordered min-width-790">
          <thead>
            <tr>
              <th width="4%">排名</th>
              <th width="10%">组别名</th>
              <th width="6%">当前经纪人</th>
              <th width="6%">累计经纪人</th>
              <th width="6%">报备客户</th>
              <th width="6%">报备成功客户</th>
              <th width="6%">到访客户</th>
              <th width="6%">认筹客户</th>
              <th width="6%">认购客户</th>
              <th width="6%">签约客户</th>
              <th width="6%">签约率</th>
              <th width="6%">签约业绩</th>
              <th width="6%">查看</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(row,index) in effectList">
              <td>{{index + 1}}</td>
              <td>{{row.groupName}}</td>
              <td>{{row.countEight}}</td>
              <td>{{row.countSeven}}</td>
              <td>{{row.countOne}}</td>
              <td>{{row.countTwo}}</td>
              <td>{{row.countThree}}</td>
              <td>{{row.countFour}}</td>
              <td>{{row.countFive}}</td>
              <td>{{row.countSix}}</td>
              <td>{{utils.calculateSignRate(row.countTwo,row.countSix)}}</td>
              <td>{{row.dealAmount}}</td>
              <td><a class="a-report" @click="gotoAgencyAgentReport(row.groupId)">查看经纪人业绩</a> </td>
            </tr>
            <tr class="result-total">
              <td colspan="2">合计</td>
              <td>{{resultTotal.countEight}}</td>
              <td>{{resultTotal.countSeven}}</td>
              <td>{{resultTotal.countOne}}</td>
              <td>{{resultTotal.countTwo}}</td>
              <td>{{resultTotal.countThree}}</td>
              <td>{{resultTotal.countFour}}</td>
              <td>{{resultTotal.countFive}}</td>
              <td>{{resultTotal.countSix}}</td>
              <td></td>
              <td>{{resultTotal.dealAmount}}</td>
              <td></td>
            </tr>
          </tbody>
        </table>
      </div>
      <div id="laypage_agency_group_report"></div>
    </div>
  </div>
</template>

<script>
import { Service, Utils, Constant } from "../../js/base";
export default {
  data() {
    return {
      params: {
        pageNo: 1,
        pageSize: 10,
        premisesId: "",
        beginDate: this.utils.getCurrentMonthFirst(),
        endDate: this.utils.getCurrentMonthLast(),
        groupName: "",
        userId: window.localStorage.getItem("userId"),
        staffCompanyId: "",
        saleStatus: "0,1,2,3",
        validateData: true,
      },
      effectList: [],
      premisesList: [],
      storeList: [],
      storeId: "",
      show: false,
      initFlag: true,
      premisesStatusEnum: [
        { saleStatus: 0, saleStatusName: "待售", saleStatusFlag: false },
        { saleStatus: 1, saleStatusName: "在售", saleStatusFlag: true },
        { saleStatus: 2, saleStatusName: "售罄", saleStatusFlag: false },
        { saleStatus: 3, saleStatusName: "下架", saleStatusFlag: false },
      ],
      showValidDataFlag: true,
      resultTotal: {
        countEight: 0,
        countSeven: 0,
        countOne: 0,
        countTwo: 0,
        countThree: 0,
        countFour: 0,
        countFive: 0,
        countSix: 0,
        dealAmount: 0,
      },
      exportBtnShow: false,
      exportUrl: "",
    };
  },
  mounted: function() {
    //加载时间与楼盘
    if (this.$route.params.premisesId) {
      this.params.premisesId = this.$route.params.premisesId;
    }
    if (this.$route.params.beginDate || this.$route.params.endDate) {
      this.params.beginDate = this.$route.params.beginDate;
      this.params.endDate = this.$route.params.endDate;
    }
    this.findPremisesLists();
    this.findStoreList();
  },
  methods: {
    findPremisesLists: function() {
      const _this = this;
      let params = {};
      params.userId = localStorage.getItem("4_userId");
      params.companyUuid = Utils.getQueryString("companyUuid");
      Service.Premise.getAgencyHisReportedPremises({ params, _this }).then(
        (rsp) => {
          if (rsp) {
            _this.premisesList = rsp;
          }
          if (_this.$route.params.premisesId) {
            _this.params.premisesId = _this.$route.params.premisesId;
            this.$forceUpdate();
          }
        }
      );
    },
    list: function(pageNo) {
      if (pageNo) {
        this.params.pageNo = 1;
      }

      var _self = this;
      var url =
        this.utilHelper.apiUrl + "/api/agencyreport/getAgencyGroupReport";
      var body = {};
      body.params = _self.params;
      body.params.beginDate = this.utils.dateFormat(
        _self.params.beginDate,
        "yyyy-MM-dd"
      );
      body.params.endDate = this.utils.dateFormat(
        _self.params.endDate,
        "yyyy-MM-dd"
      );
      body.params.storeId = _self.storeId;
      body.params.validateData = _self.showValidDataFlag;
      _self.$http.post(url, body).then((response) => {
        var body = response.body.resData;
        if (body.result == 0) {
          layer.msg("接口异常!");
        } else {
          _self.effectList = body.pageModel.rows;
          //统计数据
          _self.setResultTotoal();
          var totalPages = body.pageModel.totalPages;
          var current = body.pageModel.pageNumber;
          var total = body.pageModel.total;
          _self.show = total > 0 ? true : false;
          layui.laypage.render({
            elem: $("#laypage_agency_group_report"), //容器。值支持id名、原生dom对象，jquery对象,
            limit: _self.params.pageSize,
            curr: current,
            count: total,
            theme: "#f87242;", //皮肤
            first: "首页", //若不显示，设置false即可
            last: "尾页", //若不显示，设置false即可
            prev: "上一页", //若不显示，设置false即可
            next: "下一页", //若不显示，设置false即可
            limits: [10, 20, 50, 100, 200],
            layout: ["prev", "page", "next", "skip", "count", "limit"],
            jump: function(obj, first) {
              if (!first) {
                _self.params.pageNo = obj.curr;
                _self.params.pageSize = obj.limit;
                _self.list();
              }
            },
          });
        }
      });
    },
    findStoreList: function() {
      var _self = this;
      var url = this.utilHelper.apiUrl + "/api/getStoreByOrg";
      var body = {};
      body.params = {
        userId: window.localStorage.getItem("userId"),
        merchantId: this.$route.params.companyId,
      };
      _self.$http.post(url, body).then((response) => {
        _self.storeList = response.body.resData.list;
        if (_self.storeList.length > 0) {
          _self.storeId = _self.$route.params.storeId;
          if (!_self.storeId || !_self.initFlag) {
            _self.storeId = _self.storeList[0].id;
          }
        } else {
          _self.storeId = "";
        }
        _self.initFlag = false;
        _self.list();
      });
    },
    findReportList: function() {
      var _self = this;
      _self.params.pageNo = 1;
      _self.list();
    },
    exportInfo: function() {
      var loadIndex = layer.load();
      var _self = this;
      var body = {};
      body.params = _self.params;
      body.params.beginDate = this.utils.dateFormat(
        _self.params.beginDate,
        "yyyy-MM-dd"
      );
      body.params.endDate = this.utils.dateFormat(
        _self.params.endDate,
        "yyyy-MM-dd"
      );
      body.params.storeId = _self.storeId;
      body.params.validateData = _self.showValidDataFlag;
      var url =
        this.utilHelper.apiUrl + "/api/agencyreport/exportAgencyGroupReport";
      _self.$http
        .get(url, { params: body.params, responseType: "blob" })
        .then((response) => {
          layer.close(loadIndex);
          _self.exportBtnShow = true;
          const blob = new Blob([response.body], {
            type: "application/vnd.ms-excel", // 将会被放入到blob中的数组内容的MIME类型
          });
          const objectUrl = URL.createObjectURL(blob);
          _self.exportUrl = objectUrl;
          layer.msg("内容已生成,请【点击下载】下载!", {
            time: 2000
          });
        });
    },
    setResultTotoal() {
      var _self = this;
      _self.resultTotal = {
        countEight: 0,
        countSeven: 0,
        countOne: 0,
        countTwo: 0,
        countThree: 0,
        countFour: 0,
        countFive: 0,
        countSix: 0,
        dealAmount: 0,
      };
      for (var i in _self.effectList) {
        _self.resultTotal.countOne =
          Number(_self.resultTotal.countOne) +
          Number(_self.effectList[i].countOne);
        _self.resultTotal.countTwo =
          Number(_self.resultTotal.countTwo) +
          Number(_self.effectList[i].countTwo);
        _self.resultTotal.countThree =
          Number(_self.resultTotal.countThree) +
          Number(_self.effectList[i].countThree);
        _self.resultTotal.countFour =
          Number(_self.resultTotal.countFour) +
          Number(_self.effectList[i].countFour);
        _self.resultTotal.countFive =
          Number(_self.resultTotal.countFive) +
          Number(_self.effectList[i].countFive);
        _self.resultTotal.countSix =
          Number(_self.resultTotal.countSix) +
          Number(_self.effectList[i].countSix);
        _self.resultTotal.countSeven =
          Number(_self.resultTotal.countSeven) +
          Number(_self.effectList[i].countSeven);
        _self.resultTotal.countEight =
          Number(_self.resultTotal.countEight) +
          Number(_self.effectList[i].countEight);
        _self.resultTotal.dealAmount =
          Number(_self.resultTotal.dealAmount) +
          Number(_self.effectList[i].dealAmount);
      }
    },
    gotoAgencyAgentReport: function(groupId) {
      var _self = this;
      this.$router.push({
        name: "agencyAgentReport",
        params: {
          beginDate: this.params.beginDate,
          endDate: this.params.endDate,
          premisesId: _self.params.premisesId,
          storeId: _self.storeId,
          groupId: groupId,
          source: "group",
        },
      });
    },
  },
};
</script>